package com.xxwy.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xxwy.springcloud.entity.Dept;
import com.xxwy.springcloud.service.DeptClientService;
import com.xxwy.springcloud.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xxwy
 * on 2018/9/29 0029
 */
@RestController
@RequestMapping("dept")
public class DeptController implements DeptClientService {
    @Autowired
    private IDeptService service;

    @Override
    public boolean add(Dept dept) {
        return service.add(dept);
    }

    @Override
    @HystrixCommand(fallbackMethod = "processHystrix_Get")
    public Dept get(long id) {
        Dept dept = service.get(id);
        if (dept == null) {
            throw new RuntimeException("该Id:" + id + "没有这个部门");
        }
        return dept;
    }

    public Dept processHystrix_Get(@PathVariable Long id){
        return new Dept().setDeptno(id)
                .setDname("该ID："+id+"没有没有对应的信息,null--@HystrixCommand")
                .setDb_source("no this database in MySQL");
    }

    @Override
    public List<Dept> list() {
        return service.list();
    }


}

